package pl.edu.pw.perg.compinfodb.xls.commandline;

import java.util.LinkedList;
import java.util.List;

import pl.edu.pw.perg.compinfodb.xls.InvalidMetadataFormatException;
import pl.edu.pw.perg.compinfodb.xls.XLSMetadataReader;

public class XLSCommandlineValidator extends XLSMetadataReader{

	private static final double VERSION = 1.3;
	private List<XLSSheets> states;
	
	public static void main(String[] args) {
	  
		if (args.length == 1) {
          String path = args[0];
          if (path.equals("-v")) {
             System.out.println("Version: " + VERSION);
             System.exit(0);
          }
        }

		XLSCommandlineValidator xls = new XLSCommandlineValidator();
		try{
			xls.getFields();
			xls.getTypes();
			xls.getElementsStructure();
			xls.getConnections();
			xls.getPhysicalElements();
			
			if (xls.states.size() != 0) {
			  System.err.println("Errors in sheets:\n");
              for (XLSSheets sheet : xls.states) {
                System.err.println(sheet.getName() + "\n");
              }
            }
			else
			  System.out.println("All spreadsheets are valid.");
			
		}catch (ArrayIndexOutOfBoundsException e) {
          System.err.println("Column index out of bounds: " 
              + e.getMessage() + ". Check properties file!\n");
          
          System.exit(1);
      }catch (Exception e) {
			System.err.println("Exception occured. Please report.");
			e.printStackTrace();
			System.exit(1);
		}
		
	}
	@Override
	protected void handleFormatError(InvalidMetadataFormatException e) {
	  if(states == null)
	    states = new LinkedList<XLSSheets>();
	  if(!states.contains(e.getSheet()))
	    states.add(e.getSheet());
		System.err.println(e.getMessage());
	}
}
